/*
 * ttrss-reader-fork for Android
 * 
 * Copyright (C) 2010 N. Braden.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

/**
 * 
 */
package org.ttrssreader.widget;

import org.ttrssreader.R;
import org.ttrssreader.controllers.DBHelper;
import org.ttrssreader.gui.FeedActivity;
import org.ttrssreader.gui.FeedHeadlineActivity;
import org.ttrssreader.model.pojos.Category;
import org.ttrssreader.model.pojos.Feed;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RemoteViews;

/**
 * @author Jean Calixte
 *
 */
public class UnreadCountWidget extends AppWidgetProvider {
    private static final String TAG = "UnreadCountWidget";
    private static final int COUNT_MAX = 1000;
    
    public static void update(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        String title, itemSettings = WidgetConfigureActivity.loadIdPref(context, appWidgetId);
        int itemId, unread;
        Intent intent;
        
        if(itemSettings != null) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_unread_count);
            Log.d(TAG, itemSettings);
            String[] settings = itemSettings.split("/");
            itemId = Integer.parseInt(settings[1]);
            DBHelper helper = DBHelper.getInstance();
            Feed feed;
            Category cat;
            //ImageView icon = (ImageView) layout.findViewById(R.id.imageViewIcon);
            
            switch(WidgetConfigureActivity.Item_Type.valueOf(settings[0])) {
                case TYPE_FEED:
                    feed = helper.getFeed(itemId);
                    title = feed.title;
                    unread = feed.unread;
                    intent = new Intent(context, FeedHeadlineActivity.class);
                    intent.putExtra(FeedHeadlineActivity.FEED_ID, Integer.valueOf(settings[1]));
                    intent.putExtra(FeedHeadlineActivity.FEED_CAT_ID, feed.categoryId);
                    views.setImageViewResource(R.id.imageViewIcon, R.drawable.feedheadlinesunread48);
                    break;
                case TYPE_CAT:
                    cat = helper.getCategory(itemId);
                    title = cat.title;
                    unread = cat.unread;
                    intent = new Intent(context, FeedActivity.class);
                    intent.putExtra(FeedActivity.FEED_CAT_ID, Integer.valueOf(settings[1]));
                    views.setImageViewResource(R.id.imageViewIcon, R.drawable.categoryunread48);
                    break;
                default:
                    title = "";
                    unread = 0;
                    return;
            }
            
            views.setTextViewText(R.id.textViewUnread, unread > COUNT_MAX ? COUNT_MAX + "+": String.valueOf(unread));
            views.setTextViewText(R.id.textViewTitle, title);
            views.setViewVisibility(R.id.textViewTitle, 1);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            
            PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, 0);
            views.setOnClickPendingIntent(R.id.flUnread, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, views);           
        }
    }
    
    private void updateAll(Context context) {
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        onUpdate(context, awm, awm.getAppWidgetIds(new ComponentName(context,UnreadCountWidget.class)));
    }
    
    @Override
    public void onEnabled(Context context) {
        //super.onEnabled(context);
        //Toast.makeText(context, "onEnabled()", Toast.LENGTH_LONG).show();
        //PackageManager pm = context.getPackageManager();
        //pm.setComponentEnabledSetting(new ComponentName("org.ttrssreader", ".widget.UnreadCountUpdateService"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
        
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("org.ttrssreader.intent.action.DATA_CHANGED"))
            updateAll(context);
        
        super.onReceive(context, intent);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        
        for (int i = 0; i < appWidgetIds.length; i++)
            WidgetConfigureActivity.deleteTitlePref(context, appWidgetIds[i]);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        
        for (int i = 0; i < appWidgetIds.length; i++)
            update(context, appWidgetManager, appWidgetIds[i]);
    }
    
}
